package com.wanghangcloud.common.exception;

import com.wanghangcloud.common.bean.IResultCode;
import com.wanghangcloud.common.bean.ResultCode;
import lombok.Getter;
import org.slf4j.helpers.MessageFormatter;

/**
 * 自定义业务异常
 *
 */
@Getter
public class BusinessException extends RuntimeException {

    public String code;
    public String msg;
    public IResultCode resultCode;

    public BusinessException(IResultCode errorCode) {
        super(errorCode.getMsg());
        this.resultCode = errorCode;
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }


    public BusinessException(IResultCode errorCode, String message) {
        super(message);
        this.resultCode = errorCode;
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    public BusinessException(String code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.code = ResultCode.ERROR_PROMPT.getCode();
        this.msg = message;
    }

    public BusinessException(Throwable cause) {
        super(cause);
        this.code = ResultCode.ERROR_PROMPT.getCode();
        this.msg = cause.getMessage();
    }

    public BusinessException(String message, Object... args) {
        super(formatMessage(message, args));
        this.code = ResultCode.ERROR_PROMPT.getCode();
        this.msg = getMessage();
    }

    private static String formatMessage(String message, Object... args) {
        return MessageFormatter.arrayFormat(message, args).getMessage();
    }
}
